package ae.pcfc.cldos.online.web.controller.base.allappdtl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import ae.pcfc.cldos.online.common.delegate.AdminTaskDelegate;
import ae.pcfc.cldos.online.common.delegate.FnpGSDelegate;
import ae.pcfc.cldos.online.common.etksdb.domain.User;
import ae.pcfc.cldos.online.common.vo.AppMetaDataVO;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BaseController;
import ae.pcfc.cldos.online.web.exception.ResourceNotFoundException;
import ae.pcfc.cldos.online.web.factories.AllAppDtlUrlFactory;

@Controller
@RequestMapping(ViewPath.__ALLAPPDTL)
public class BaseAllAppDtlController extends BaseController {

	@Autowired
	@Qualifier("adminTaskDelegate")
	private AdminTaskDelegate adminTaskDelegate;
	
	@Autowired
	@Qualifier("FnpGSDelegate")
	private FnpGSDelegate fnpGSDelegate;
	
	@RequestMapping
	public ModelAndView forwardToCorrectTaskHandler(@RequestParam String appId) throws Throwable{
		
		AppMetaDataVO appMetaDataVO = adminTaskDelegate.getPlainAppMetaDataVO(
				encryptionDelegate.decodeAndDecrypt(appId));
		if(appMetaDataVO==null){
			throw new ResourceNotFoundException();
		}
		
	/*	User user = fnpGSDelegate.findUserByUserName(securityService.getLoginedUsername());
		if(!appMetaDataVO.getClientId().equals(user.getClientId())){
			throw new ResourceNotFoundException();
		}*/
		
		String forwardUrl = AllAppDtlUrlFactory.getUrlByAppMetaDataVO(appMetaDataVO);
		if(forwardUrl==null || forwardUrl.isEmpty()){
			throw new ResourceNotFoundException();
		}
		
		return new ModelAndView(ViewPath.__FORWARD+forwardUrl);
	}
}
